<?php

namespace Apps\AppApi\Method\MyBranch;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Enum\SearchDataType;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\SearchASync;
use Apps\Common\Libs\UploadPic;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosQKeyword;
use Phalcon\Mvc\Model;
use Apps\Common\Models\BeautyParlorInteractive;


/**
 * 添加美容院互动
 * @package Apps\AppApi\Method\MyBranch
 */
class AddInteracive extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\BeautyParlorI
     */
    private $success = 0;
    private $id = 0;
    private $message;

    public function run()
    {
    	/**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }

        $bp_id = $this->getDataItem('bp_id', '');
        $content = Keyword::Filter($this->getDataItem('content',''));

        $up = new UploadPic();
        $pics = $up->beautyParlorInteractive();
        
        if ($pics) {
            $pics = json_encode($pics);
        } else {
            $pics = json_encode([]);
        }

        if(!empty($content) && $bp_id != '0') {
            $q = new BeautyParlorInteractive();
            $q->user_id = $this->getUserAuth()->userId;
            $q->bp_id = $bp_id;

            //关键字替换
            $keyword = CosQKeyword::query() -> execute() -> toArray();
            foreach ($keyword as $k => $v){
            	 
            	$content = str_replace($v['keyword'], '***', $content);
                   
            }
            // 过滤表情
            $q->content = preg_replace_callback('/[\xf0-\xf7].{3}/', function($r) { return ''; }, $content);
            $q->pics = $pics;
            $q->state = 1;
            $date = date('Y-m-d H:i:s');
            $q->addtime = $date;

            if($q->save()){
                $this->id = $q->id;
                $this->success = 1;
                $this->message = "添加美粒互动成功";
            } else {
            	$this->success = 0;
                $this->message = "添加美粒互动失败";
            }
        } else {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, "");
        }

        $this->setResult([
            'id' => $this->id,
            'success' => $this->success,
            'message' =>$this->message
        ]);
    }

}